#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021-01-27 17:08
# @Author  : anan
# @Email   : mfkdgefhn@163.com
# @File    : biquyxItem.py
import json

import scrapy

from biquyx.items import BiquyxItem
import requests
import os


class biquyxItemSpider(scrapy.Spider):
    """ 123 """
    name = 'biquyxItem'
    allowed_domains = ['biquyx.com']
    start_urls = 'https://www.biquyx.com/Book/getJino.html'

    def start_requests(self):
        """ 123 """
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        data = {"type": "mh", "id": "48", "p": "1"}
        yield scrapy.FormRequest(url=self.start_urls,
                                 formdata=data,
                                 headers={"Content-Type": "application/x-www-form-urlencoded"},
                                 callback=self.parse)

    def parse(self, response):
        """ 123 """
        print(111111111111111111111)
        print(response.text)
        tr_list = response.xpath('//div[@class="item"]')
        for tr in tr_list:
            img = tr.xpath('./a/@href').extract_first()
            if 'http' in img:
                url = img
            else:
                url = 'https://www.biquyx.com' + img
            download_img(url, 'E:\project\python\biquyx\biquyx\imgs\偷窥(全集)')

        yield self


def download_img(img_url, path):
    """ 123 """
    r = requests.get(img_url, stream=True)
    if r.status_code == 200:
        open(path, 'wb').write(r.content)
        print("done")
    del r